package com.baizhi.aspect;

import com.baizhi.AdminLogAnnoation.AdminLogAnnotion;
import com.baizhi.dao.CmfzAdminDao;
import com.baizhi.dao.CmfzLogDao;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.entity.CmfzLog;

import com.baizhi.ipk.IPKit;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.RequestHandledEvent;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;


/**
 * @Author: renpu
 * @Date: 2019/3/8 20:02
 * @Description:
 */

@Configuration
@Aspect
public class LogAspect {
    //创建Logger的工厂获取Logger对象；
    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Autowired
    private CmfzLogDao cmfzLogDao;

    @Autowired
    private CmfzAdminDao cmfzAdminDao;

    //将注解作为切点进行切入；
    @Pointcut("@annotation(com.baizhi.AdminLogAnnoation.AdminLogAnnotion)")
    public void logPoint(){

    }




    //创建一个后置增强，日志记录；
    @Before("logPoint()")
    public void Log(JoinPoint joinPoint){

        //创建日志对象；
        CmfzLog cmfzLog=new CmfzLog();

        //获取session中的对象数据存储到Log对象里
        ServletRequestAttributes rr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = rr.getRequest();
        HttpSession session = request.getSession();
        /*CmfzAdmin admin = (CmfzAdmin) session.getAttribute("admin");
        String userName=admin.getUserName();*/
        Subject subject = SecurityUtils.getSubject();
        String userName =(String) subject.getPrincipal();
        if(userName!=null){
            cmfzLog.setAdminUserName(userName);

            session.setAttribute("userName",userName);
            logger.info(userName);

            //将时间封装到Logger对象里；
            cmfzLog.setLogDate(new Date());

            //通过连接点对象获取方法签名；
            MethodSignature methodSignature=(MethodSignature) joinPoint.getSignature();

            //通过方法签名获取到方法
            Method method=methodSignature.getMethod();

            //通过方法获取到注解对象；
            AdminLogAnnotion adminLogAnnotion=method.getAnnotation(AdminLogAnnotion.class);

            //将获取到的内容赋值给Log对象；
            cmfzLog.setLogContent(adminLogAnnotion.value());

            //获取用访问的IP地址；
            String ipAddrByRequest=IPKit.getIpAddrByRequest(request);

            cmfzLog.setLogIp(ipAddrByRequest);

            cmfzLogDao.addLog(cmfzLog);
        }
    }
}
